how to use v2ray on centos7

  1. add firewall(optional)

    firewalld

    firewall-cmd --load-zone-defaults=public --permanent
    firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address=yourIpAddress port protocol='tcp' port='22' accept"
    firewall-cmd --remove-service=ssh --permanent
    firewall-cmd --reload
    

    iptables

    yum install iptables-services -y
    iptables -F
    iptables -A INPUT -s yourIpAddress -p tcp -m tcp --dport 22 -j ACCEPT
    iptables -A INPUT -p tcp -m tcp --dport 22 -j DROP
    service iptables save && service iptables restart
    
  2. open swap(optional)

    dd if=/dev/zero of=/swap bs=1M count=2048
    mkswap /swap
    chmod 0600 /swap
    swapon /swap
    echo vm.swappiness=80 >> /etc/sysctl.conf
    sysctl --system
    
  3. ensure selinux is disabled

    sed -i 's/^SELINUX=.*$/SELINUX=disabled/g' /etc/selinux/config
    setenforce 0
    
  4. install v2ray server

    yum install yum-utils -y
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    yum install docker-ce -y
    systemctl start docker && systemctl enable docker
    docker pull v2ray/official
    mkdir -p /etc/v2ray
    cat << EOF > /etc/v2ray/config.json
    {
      "inbounds": [
        {
          "port": 10800,
          "listen":"0.0.0.0",
          "protocol": "vmess",
          "settings": {
            "clients": [
              {
                "id": "bffce3d2-3c89-4cfc-989b-baca4708a477",
                "alterId": 70
              }
            ]
          },
          "streamSettings": {
            "network": "ws",
            "wsSettings": {
            "path": "/chenshi"
            }
          }
        }
      ],
      "outbounds": [
        {
          "protocol": "freedom",
          "settings": {}
        }
      ]
    }
    EOF
    docker run -d --name v2ray -v /etc/v2ray:/etc/v2ray -p 127.0.0.1:10800:10800 v2ray/official  v2ray -config=/etc/v2ray/config.json
    
  5. install nginx

    yum install epel-release -y
    yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
    yum install nginx certbot -y
    # replace chenshi.net with your domain
    certbot certonly --standalone -d chenshi.net --register-unsafely-without-email --agree-tos
    cat << EOF > /etc/nginx/conf.d/v2ray.conf
    server {
      listen  443 ssl;
      ssl on;
      ssl_certificate       /etc/letsencrypt/live/chenshi.net/fullchain.pem;
      ssl_certificate_key   /etc/letsencrypt/live/chenshi.net/privkey.pem;
      ssl_protocols         TLSv1 TLSv1.1 TLSv1.2;
      ssl_ciphers           HIGH:!aNULL:!MD5;
      server_name           chenshi.net;
            location /chenshi {
            proxy_redirect off;
            proxy_pass http://127.0.0.1:10800;
            proxy_http_version 1.1;
            proxy_set_header Upgrade \$http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Host \$http_host;
    
            # Show realip in v2ray access.log
            proxy_set_header X-Real-IP \$remote_addr;
            # proxy_set_header Host \$host;
            proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
            }
    }
    EOF
    systemctl start nginx && systemctl enable nginx
    crontab -l |(cat;echo "0 0 1 * * systemctl stop nginx && certbot renew;systemctl start nginx")| crontab -
    
  6. CDN

    you should use it by yourself.

  7. use v2ray client

    use this configure file on your pc:

    {
      "inbounds": [
        {
          "port": 1080,
          "listen": "0.0.0.0",
          "protocol": "socks",
          "sniffing": {
            "enabled": true,
            "destOverride": ["http", "tls"]
          },
          "settings": {
            "auth": "password",
            "accounts": [
        {
          "user": "chenshi.net",
          "pass": "chenshi.net"
        }
      ],
            "udp": false
          }
        }
      ],
      "outbounds": [
        {
          "protocol": "vmess",
          "settings": {
            "vnext": [
              {
                "address": "chenshi.net",
                "port": 443,
                "users": [
                  {
                    "id": "bffce3d2-3c89-4cfc-989b-baca4708a477",
                    "alterId": 70
                  }
                ]
              }
            ]
          },
          "streamSettings": {
            "network": "ws",
            "security": "tls",
            "wsSettings": {
              "path": "/chenshi"
            }
          }
        }
      ]
    }